package com.xinghou.platform.lucene.main.impl;

import com.xinghou.platform.dao.bases.util.StringUtils;
import com.xinghou.platform.lucene.main.UserLuceneService;
import com.xinghou.platform.lucene.main.bean.SolrServerFactory;
import com.xinghou.platform.lucene.main.bean.StoreSolr;
import com.xinghou.platform.lucene.main.bean.UserSolr;
import com.xinghou.platform.lucene.main.util.SolrJOper;
import com.xinghou.platform.lucene.main.util.SolrQuery;
import com.xinghou.platform.model.main.Store;
import com.xinghou.platform.model.main.StoreQueryParam;
import com.xinghou.platform.model.main.User;
import com.xinghou.platform.model.main.UserQueryParam;
import com.xinghou.platform.page.Pagination;
import com.xinghou.platform.util.SolrUtil;
import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.SolrServerException;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by qingshengzheng on 16/4/27.
 */
@Service("userSolrService")
public class UserSolrServiceImpl implements UserLuceneService {
    private SolrJOper<UserSolr> solrOper = new SolrJOper<UserSolr>();
    private SolrServerFactory solrServerFactory = SolrServerFactory.instance();

    @Override
    public List<User> getSearchUserInfos(String key, int fromno, int pagesize, String userType, String provinceCode) throws IOException {
        SolrQuery solrQuery = new SolrQuery();
        String searchKey = solrQuery.like("nickname", key).toString();
        SolrClient server = solrServerFactory.getUserServer();
        Pagination<UserSolr> values = solrOper.queryBean(server, searchKey, fromno, pagesize, UserSolr.class);
        if (values==null||values.getValues() == null || values.getValues().isEmpty()) return null;
        List<User> users = new ArrayList<User>();
        List<UserSolr> solrs = values.getValues();
        for (int i = 0; i < solrs.size(); i++) {
            User temp = solrs.get(i).toUser();
            users.add(temp);
        }
        return users;
    }
    @Override
    public List<User> getSearchUserInfos(String key, int fromno, int pagesize, String userType, String citycode, Double distince, double lng, double lat) throws IOException {
        HashMap map = new HashMap();
        SolrQuery query = new SolrQuery();
        String searchKey = query.like(SolrQuery.OPER_AND,"nickname", key).equal(SolrQuery.OPER_AND,"citycode",citycode).toString();
        SolrClient server = solrServerFactory.getUserServer();
        Pagination<UserSolr> values = solrOper.queryLbsBean(server, searchKey, distince, "position", lat + "," + lng, fromno, pagesize, UserSolr.class);
        if (values==null||values.getValues() == null || values.getValues().isEmpty()) return null;
        List<User> users = new ArrayList<User>();
        List<UserSolr> solrs = values.getValues();
        for (int i = 0; i < solrs.size(); i++) {
            User temp = solrs.get(i).toUser();
            users.add(temp);
        }
        return users;

    }
    @Override
    public List<User> queryUserByParams(UserQueryParam param, int fromno, int pagesize, double distinct, double lon, double lat) {
        SolrQuery query = new SolrQuery();
        if (param.hasCard != null) {
                query.equal(SolrQuery.OPER_AND, "hascard", param.hasCard.toString());
        }
        if (param.sex != null)
            query.equal(SolrQuery.OPER_AND, "sex", param.sex.toString());
        if (param.minageyear != null||param.maxageyear!=null)
            query.between(SolrQuery.OPER_AND, "ageyear", param.minageyear + "", param.maxageyear + "");
        if (!StringUtils.isNullOrEmpty(param.nickname))
            query.like(SolrQuery.OPER_AND, "nickname", param.nickname);
        String key = query.toString();
        SolrClient server = solrServerFactory.getUserServer();
        Pagination<UserSolr> values = solrOper.queryLbsBean(server, key, distinct, "position", lat + "," + lon, fromno, pagesize, UserSolr.class);
        if (values==null||values.getValues() == null || values.getValues().isEmpty()) return null;
        List<User> users = new ArrayList<User>();
        List<UserSolr> solrs = values.getValues();
        for (int i = 0; i < solrs.size(); i++) {
            User temp = solrs.get(i).toUser();
            users.add(temp);
        }
        return users;
    }

    @Override
    public int saveUserInfo(User user) {
        SolrClient server = solrServerFactory.getUserServer();
        solrOper.addBean(server, UserSolr.fromUser(user));
        return 0;
    }

    @Override
    public int updateUserInfo(User user) {
        SolrClient server = solrServerFactory.getUserServer();
        try {
            server.deleteById(user.getUserid() + "");
            solrOper.addBean(server, UserSolr.fromUser(user));
        } catch (SolrServerException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return 0;
    }
}
